package com.jt.common.util;

import org.apache.commons.io.FileUtils;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @Author: Jingyan
 * @Time: 2017-11-22 12:20
 * @Description: 文件工具类
 */
public class FileUtil {

    private static final String ENCODING_UTF8 = "UTF-8";

    /**
     * Created with Jingyan
     * Time: 2017-11-22 14:01
     * Description: 写文件操作
     */
    public static String writeStringToFile(String filepath, String filename, String data) throws Exception {
        File file = new File(filepath, filename);
        try {
            FileUtils.writeStringToFile(file, data, ENCODING_UTF8);
            return file.getAbsolutePath();
        } catch (IOException e) {
            e.printStackTrace();
            throw new Exception("文件写操作异常！");
        }
    }

    /**
     * @Author: jingyan
     * @Time: 2017/9/6 16:48
     * @Describe: 写入文件
     * @Param filePath:文件全路径
     * @Param content:文件内容
     * @Param appendable:是否追加写入
     */
    public static String writeFileByNio(String filePath, String content, boolean appendable) {
        if (null == filePath || "".equals(filePath)) {
            return null;
        }
        FileOutputStream fos = null;
        FileChannel channel = null;
        try {
            File file = new File(filePath);
            // 如果父目录不存在，创建父目录
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            fos = new FileOutputStream(file, appendable);
            channel = fos.getChannel();
            ByteBuffer buf = ByteBuffer.wrap(content.getBytes());
            channel.write(buf);
            return filePath;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != channel) {
                    channel.close();
                }
                if (null != fos) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * @Author: jingyan
     * @Time: 2017/9/6 17:54
     * @Describe: 读取文件
     * @Param filePath:文件全路径
     */
    public static String readFileByNio(String filePath) {
        if (null == filePath || "".equals(filePath)) {
            return null;
        }
        FileInputStream fis = null;
        FileChannel channel = null;
        try {
            fis = new FileInputStream(filePath);
            channel = fis.getChannel();
            ByteBuffer buffer = ByteBuffer.allocate((int) channel.size());
            channel.read(buffer);
            buffer.flip();
            String content = new String(buffer.array(), "utf-8");
            buffer.clear();
            return content;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                channel.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

}